﻿/// <summary>
/// Author: HBLAi
/// Date: 2013/06/06
/// </summary>
using System.Linq;
using Ivs.Core.Mapping;
using SketchTour.Models;
using System.Collections.Generic;
using Ivs.Core.Common.Utilities;
using System;

namespace SketchTour.DAL
{
    public class MSVH00DAL
    {
        private EFBaseDao _context;
        public MSVH00DAL()
        {
            _context = new EFBaseDao();
        }

        public IQueryable<MSVH00Models> GetVehiclesView()
        {
            var query = from v in _context.m_vehicle
                        select new MSVH00Models
                        {
                            Code = v.Code,
                            VehicleTypeID = v.m_vehicletype.ID,
                            VehicleType = v.m_vehicletype.Name,
                            Name = v.Name,
                            Address = v.Address,
                            Tel = v.Tel,
                            Fax = v.Fax,
                            Note = v.Note,
                            CreateBy = v.CreateBy,
                            CreateDate = v.CreateDate,
                            UpdateBy = v.UpdateBy,
                            UpdateDate = v.UpdateDate
                        };
            return query;
        }

        public MSVH00Models Search(string code)
        {
            return GetVehiclesView().Where(v => v.Code == code).Single();
        }

        public IQueryable<m_vehicletype> GetVehicleType()
        {
            return _context.m_vehicletype;
        }

        public int Insert(MSVH00Models m_vehicle)
        {
            int Results; 
            m_vehicle obj = new m_vehicle();
            obj.Code = m_vehicle.Code;
            obj.Name = m_vehicle.Name;
            obj.Address = m_vehicle.Address;
            obj.CreateDate = _context.GetDateTime();
            obj.Tel = m_vehicle.Tel;
            obj.Fax = m_vehicle.Fax;
            obj.Note = m_vehicle.Note;
            obj.VehicleTypeID = m_vehicle.VehicleTypeID;
            _context.m_vehicle.AddObject(obj);
            Results = _context.Saves();
            return Results;
        }

        public int Update(MSVH00Models m_vehicle)
        {
            int Results;
            var obj = _context.m_vehicle.Single(v => v.Code == m_vehicle.Code);
            obj.Code = m_vehicle.Code;
            obj.Name = m_vehicle.Name;
            obj.Address = m_vehicle.Address;
            obj.CreateDate = _context.GetDateTime();
            obj.Tel = m_vehicle.Tel;
            obj.Fax = m_vehicle.Fax;
            obj.Note = m_vehicle.Note;
            obj.VehicleTypeID = m_vehicle.VehicleTypeID;
            Results = _context.Saves();
            return Results;
        }

        public int Delete(string id)
        {
            m_vehicle obj = _context.m_vehicle.Single(v => v.Code == id);
            _context.m_vehicle.DeleteObject(obj);
            int Results = _context.Saves();
            return Results;
        }

        #region Search
        public int Search(MSVH00Models Model, out IEnumerable<MSVH00Models> data)
        {
            int returnCode = 0;
            try
            {
                data = from v in _context.m_vehicle
                       select new MSVH00Models
                       {
                           Code = v.Code,
                           VehicleTypeID = v.m_vehicletype.ID,
                           VehicleType = v.m_vehicletype.Name,
                           Name = v.Name,
                           Address = v.Address,
                           Tel = v.Tel,
                           Fax = v.Fax,
                           Note = v.Note,
                           CreateBy = v.CreateBy,
                           CreateDate = v.CreateDate,
                           UpdateBy = v.UpdateBy,
                           UpdateDate = v.UpdateDate

                       };
                if (!Utilities.isNullOrEmpty(Model.Code))
                {
                    data = data.Where(a => a.Code.Contains(Model.Code));
                }
                if (Model.VehicleTypeID.HasValue)
                {
                    data = data.Where(a => a.VehicleTypeID == Model.VehicleTypeID);
                }
                if (!Utilities.isNullOrEmpty(Model.Tel))
                {
                    data = data.Where(a => a.Tel.Contains(Model.Tel));
                }
                if (!Utilities.isNullOrEmpty(Model.Name))
                {
                    data = data.Where(a => a.Name.Contains(Model.Name));
                }
                if (!Utilities.isNullOrEmpty(Model.Address))
                {
                    data = data.Where(a => a.Address.Contains(Model.Address));
                }
                data = data.OrderByDescending(s => s.Code);
                returnCode = data.Count();
            }
            catch (Exception ex)
            {
                returnCode = _context.ProcessDbException(ex);
                data = null;
            }
            return returnCode;

        }
        #endregion Search
    }
}